package cn.iocoder.yudao.module.nginxui.service.globalblock;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.nginxui.controller.admin.globalblock.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.globalblock.GlobalBlockDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nginxui.dal.mysql.globalblock.GlobalBlockMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.nginxui.enums.ErrorCodeConstants.*;

/**
 * Nginx全局块配置 Service 实现类
 *
 * @author HuangJunZhong
 */
@Service
@Validated
public class GlobalBlockServiceImpl implements GlobalBlockService {

    @Resource
    private GlobalBlockMapper globalBlockMapper;

    @Override
    public Long createGlobalBlock(GlobalBlockSaveReqVO createReqVO) {
        // 插入
        GlobalBlockDO globalBlock = BeanUtils.toBean(createReqVO, GlobalBlockDO.class);
        globalBlockMapper.insert(globalBlock);

        // 返回
        return globalBlock.getId();
    }

    @Override
    public void updateGlobalBlock(GlobalBlockSaveReqVO updateReqVO) {
        // 校验存在
        validateGlobalBlockExists(updateReqVO.getId());
        // 更新
        GlobalBlockDO updateObj = BeanUtils.toBean(updateReqVO, GlobalBlockDO.class);
        globalBlockMapper.updateById(updateObj);
    }

    @Override
    public void deleteGlobalBlock(Long id) {
        // 校验存在
        validateGlobalBlockExists(id);
        // 删除
        globalBlockMapper.deleteById(id);
    }

    @Override
        public void deleteGlobalBlockListByIds(List<Long> ids) {
        // 删除
        globalBlockMapper.deleteByIds(ids);
        }


    private void validateGlobalBlockExists(Long id) {
        if (globalBlockMapper.selectById(id) == null) {
            throw exception(GLOBAL_BLOCK_NOT_EXISTS);
        }
    }

    @Override
    public GlobalBlockDO getGlobalBlock(Long id) {
        return globalBlockMapper.selectById(id);
    }

    @Override
    public PageResult<GlobalBlockDO> getGlobalBlockPage(GlobalBlockPageReqVO pageReqVO) {
        return globalBlockMapper.selectPage(pageReqVO);
    }

}